const commentService = require('../service/comment.service');

class CommentController {

    //用户评论接口
    async create(ctx, next) {
        try {
            const user_id = ctx.user.id;
            const {
                content,
                moment_id
            } = ctx.request.body;
            const result = await commentService.createService(user_id, content, moment_id);
            ctx.body = result;
        } catch (err) {
            console.log(err);
        }
    }

    async reply(ctx, next) {
        try {
            const user_id = ctx.user.id;
            const {
                content,
                moment_id
            } = ctx.request.body;
            const {
                comment_id
            } = ctx.params;
            const result = await commentService.replyService(moment_id, content, user_id, comment_id);
            ctx.body = result;
        } catch (err) {
            console.log(err);
        }
    }

    async update(ctx, next) {
        try {
            const {
                commentId
            } = ctx.params;
            const {
                content
            } = ctx.request.body;
            const result = await commentService.updateService(commentId, content);
            ctx.body = result;
        } catch (err) {
            console.log(err);
        }
    }

    async remove(ctx, next) {
        try {
            const {
                commentId
            } = ctx.params;
            console.log(commentId);
            const result = await commentService.removeService(commentId);
            ctx.body = result;
        } catch (err) {
            console.log(err);
        }
    }

    async list(ctx, next) {
        try {
            // console.log(ctx.query);    //[Object: null prototype] { 'momentId ': ' 3' }
            const {
                momentId
            } = ctx.query;
            const result = await commentService.getCommentsByMomentId(momentId);
            ctx.body = result;
        } catch (err) {
            console.log(err);
        }
    }
}

module.exports = new CommentController();